Skip to content

Smith series tolerance of the truncated pullbacks scaling like ΔA - #328

Merged
leburgel merged 7 commits into
mainfrom
lb/smith_rtol
Nov 26, 2026
Merged

leburgel merged 7 commits into
mainfrom
lb/smith_rtol

Conversation

@leburgel

@leburgel leburgel commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

svd_trunc_pullback!, eigh_trunc_pullback! and eig_trunc_pullback! stop the Smith series for the part outside the kept vectors once its terms drop below degeneracy_atol. That tolerance scales like the singular values or eigenvalues, while the terms scale like ΔA, i.e. inversely with them. The result therefore depends on the scale of A: for A scaled by 2^20 or more, the series stops after too few terms and ΔA is off by 1e-3 (svd, eigh) up to 2e-1 (eig) on the reproducer below.

This PR adds a keyword complement_atol to the three pullbacks. Its default, default_pullback_complement_atol, is eps(T)^(3/4) times the largest entry of the cotangents that scale like ΔA: ΔD and ΔV ./ transpose(D) (eig, eigh), or ΔS, ΔU ./ transpose(S) and ΔVᴴ ./ S (svd). The pullbacks then scale exactly with A, with errors of 1e-15 (eig: 5e-12) at every scale.

Reproducer

c ΔA(c A) against the full pullback at A, with c a power of two so the factors scale exactly (ΔS / c for c A), for spectra with kept values in [1, 10] and discarded ones at most 0.5 (main against this PR, Julia 1.12):

# The truncated pullbacks of A and of c A, c a power of two (the factors scale exactly): with the cotangents of the
# same function (ΔS / c for c A), c ΔA(c A) should equal ΔA(A), which the full pullback gives as reference.
# Spectra with a clear gap: kept values in [1, 10], discarded at most 0.5 (eig: by magnitude, complex).
using LinearAlgebra, Random, Printf
using MatrixAlgebraKit
using MatrixAlgebraKit: svd_trunc_pullback!, svd_pullback!, eigh_trunc_pullback!, eigh_pullback!,
    eig_trunc_pullback!, eig_pullback!

relerr(a, b) = norm(a - b) / norm(b)
unitary(rng, n) = Matrix(qr(randn(rng, ComplexF64, n, n)).Q)
const CS = (2.0^-40, 2.0^-20, 1.0, 2.0^20, 2.0^40)

function report(name, f, ref)
    errs = [c => relerr(c * f(c), ref) for c in CS]
    println(@sprintf("%-22s", name), join([@sprintf("c = 2^%-3d %.1e", round(Int, log2(c)), e) for (c, e) in errs], "   "))
    return flush(stdout)
end

function svd_case(m, n, k; rng = Xoshiro(1))
    r = min(m, n)
    σ = vcat(sort(1 .+ 9 .* rand(rng, k); rev = true), sort(0.5 .* rand(rng, r - k); rev = true))
    U, V = unitary(rng, m)[:, 1:r], unitary(rng, n)[:, 1:r]
    A = U * Diagonal(σ) * V'
    ΔU, ΔS, ΔVᴴ = randn(rng, ComplexF64, m, k), randn(rng, k), randn(rng, ComplexF64, k, n)
    trunc(c) = svd_trunc_pullback!(
        zeros(ComplexF64, m, n), c * A, (U[:, 1:k], Diagonal(c * σ[1:k]), V[:, 1:k]'),
        (copy(ΔU), Diagonal(ΔS / c), copy(ΔVᴴ)); gauge_atol = Inf
    )
    ref = svd_pullback!(
        zeros(ComplexF64, m, n), A, (U, Diagonal(σ), V'),
        (hcat(ΔU, zeros(m, r - k)), Diagonal(vcat(ΔS, zeros(r - k))), vcat(ΔVᴴ, zeros(r - k, n))); gauge_atol = Inf
    )
    return report("svd $m×$n, k = $k", trunc, ref)
end

function eigh_case(n, k; rng = Xoshiro(2))
    λ = vcat((1 .+ 9 .* rand(rng, k)) .* rand(rng, (-1, 1), k), 0.5 .* (2 .* rand(rng, n - k) .- 1))
    Q = unitary(rng, n)
    A = Hermitian(Q * Diagonal(λ) * Q') |> Matrix
    ΔD, ΔV = randn(rng, k), randn(rng, ComplexF64, n, k)
    trunc(c) = eigh_trunc_pullback!(
        zeros(ComplexF64, n, n), c * A, (Diagonal(c * λ[1:k]), Q[:, 1:k]), (Diagonal(ΔD / c), copy(ΔV)); gauge_atol = Inf
    )
    ref = eigh_pullback!(
        zeros(ComplexF64, n, n), A, (Diagonal(λ), Q), (Diagonal(vcat(ΔD, zeros(n - k))), hcat(ΔV, zeros(n, n - k)));
        gauge_atol = Inf
    )
    return report("eigh $n×$n, k = $k", trunc, ref)
end

function eig_case(n, k; rng = Xoshiro(3))
    phase(j) = cis.(2π .* rand(rng, j))
    λ = vcat((1 .+ 9 .* rand(rng, k)) .* phase(k), 0.5 .* rand(rng, n - k) .* phase(n - k))
    W = randn(rng, ComplexF64, n, n) + 4I # well conditioned
    W ./= sqrt.(sum(abs2, W; dims = 1))
    A = W * Diagonal(λ) / W
    ΔD, ΔV = randn(rng, ComplexF64, k), randn(rng, ComplexF64, n, k)
    # gauge invariant (vᵢᴴ Δvᵢ = 0), so that the full and the truncated pullback do not drop different gauge parts
    ΔV .-= W[:, 1:k] .* sum(conj.(W[:, 1:k]) .* ΔV; dims = 1)
    trunc(c) = eig_trunc_pullback!(
        zeros(ComplexF64, n, n), c * A, (Diagonal(c * λ[1:k]), W[:, 1:k]), (Diagonal(ΔD / c), copy(ΔV)); gauge_atol = Inf
    )
    ref = eig_pullback!(
        zeros(ComplexF64, n, n), A, (Diagonal(λ), W), (Diagonal(vcat(ΔD, zeros(n - k))), hcat(ΔV, zeros(n, n - k)));
        gauge_atol = Inf
    )
    return report("eig $n×$n, k = $k", trunc, ref)
end

println("=== ", ARGS[1], ": ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖")
svd_case(150, 200, 20)
svd_case(200, 150, 20)
eigh_case(200, 20)
eig_case(200, 20)
=== main: ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖
svd 150×200, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   4.5e-14   c = 2^20  1.7e-03   c = 2^40  1.7e-03
svd 200×150, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   5.4e-14   c = 2^20  2.7e-03   c = 2^40  2.7e-03
eigh 200×200, k = 20  c = 2^-40 8.6e-16   c = 2^-20 8.6e-16   c = 2^0   1.1e-15   c = 2^20  2.4e-03   c = 2^40  2.4e-03
eig 200×200, k = 20   c = 2^-40 4.6e-12   c = 2^-20 4.6e-12   c = 2^0   4.6e-12   c = 2^20  4.2e-05   c = 2^40  2.1e-01
=== this PR: ‖c ΔA(c A) − ΔA_full(A)‖ / ‖ΔA_full(A)‖
svd 150×200, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   1.2e-15   c = 2^20  1.2e-15   c = 2^40  1.2e-15
svd 200×150, k = 20   c = 2^-40 1.2e-15   c = 2^-20 1.2e-15   c = 2^0   1.2e-15   c = 2^20  1.2e-15   c = 2^40  1.2e-15
eigh 200×200, k = 20  c = 2^-40 1.1e-15   c = 2^-20 1.1e-15   c = 2^0   1.1e-15   c = 2^20  1.1e-15   c = 2^40  1.1e-15
eig 200×200, k = 20   c = 2^-40 4.6e-12   c = 2^-20 4.6e-12   c = 2^0   4.6e-12   c = 2^20  4.6e-12   c = 2^40  4.6e-12

🤖 Generated with Claude Code

leburgel and others added 2 commits November 18, 2026 13:51
…ht-hand side (stein_rtol) instead of degeneracy_atol

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel
leburgel requested a review from Jutho November 18, 2026 14:46
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl 66.66% 1 Missing ⚠️
src/common/defaults.jl 94.44% 1 Missing ⚠️
src/pullbacks/eig.jl 50.00% 1 Missing ⚠️
src/pullbacks/eigh.jl 50.00% 1 Missing ⚠️
src/pullbacks/svd.jl 66.66% 1 Missing ⚠️
Files with missing lines Coverage Δ
...ixAlgebraKitEnzymeExt/MatrixAlgebraKitEnzymeExt.jl 77.91% <66.66%> (+77.91%) ⬆️
src/common/defaults.jl 82.14% <94.44%> (+17.85%) ⬆️
src/pullbacks/eig.jl 94.31% <50.00%> (ø)
src/pullbacks/eigh.jl 86.90% <50.00%> (ø)
src/pullbacks/svd.jl 95.02% <66.66%> (ø)

... and 14 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/pullbacks/eigh.jl Resolved
@leburgel leburgel changed the title Smith series tolerance of the truncated pullbacks relative to the right-hand side Smith series tolerance of the truncated pullbacks scaling like ΔA Nov 25, 2026
Comment thread src/common/defaults.jl Resolved
Comment thread src/common/defaults.jl Resolved
Comment thread src/common/defaults.jl Resolved

@Jutho Jutho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside for one more suggestion, approvid.

@Jutho

Jutho commented Nov 26, 2026

Copy link
Copy Markdown
Member

Not sure if the test failures are caused by this PR. There seem to be some NaNs being generated in the AD tests. On main, Enzyme tests also fail, but due to timeout, not due to errors, it seems. If this is unrelated, feel free to merge.

@leburgel

Copy link
Copy Markdown
Member Author

I think this caused an actual NaN failure in the Enzyme tests, so I'll have to sort that out first.

@leburgel

leburgel commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

Switching all pullback defaults to eps(T)^(3/4) * norm broke the Enzyme tests for eig_trunc with a complex Diagonal input. For complex Diagonal input, D shares its storage with A, and the truncated eig/eigh Enzyme rule didn't copy it. So when call_and_zero! zeroed A, the pullback saw D == 0. The old eps(0.0)^(3/4) is still positive, so inv_safe returned 0 and the gradient happened to come out right. The new tolerance is exactly 0, so the gradient became NaN.

I added the same has_equal_storage check that the full-decomposition rule already uses, is this a reasonable fix @kshyatt?

@leburgel
leburgel requested a review from kshyatt November 26, 2026 14:34
@kshyatt

kshyatt commented Nov 26, 2026

Copy link
Copy Markdown
Member

Seems reasonable to me. On a separate PR we should make that more generic in its own function.

@leburgel
leburgel merged commit 2433e7c into main Nov 26, 2026
33 of 34 checks passed
@leburgel
leburgel deleted the lb/smith_rtol branch November 26, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants